package com.qf.android.sport.adapter.headline;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qf.android.sport.R;
import com.qf.android.sport.databinding.LayoutHeadlineDynamicImageItemBinding;

import java.util.List;

/**
 * Created by wen10 on 2016/12/16.
 */

public class HeadlineDynamicImgRecyclerAdapter extends RecyclerView.Adapter<HeadlineDynamicImgRecyclerAdapter.ViewHolder> {
    private List<String> listString;
    private int variableId;

    public HeadlineDynamicImgRecyclerAdapter(List<String> listString, int variableId) {
        this.listString = listString;
        this.variableId = variableId;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.layout_headline_dynamic_image_item, parent, false).getRoot();
        ViewDataBinding bind = DataBindingUtil.bind(rootView);
        ViewHolder holder = new ViewHolder(bind);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        LayoutHeadlineDynamicImageItemBinding binding = (LayoutHeadlineDynamicImageItemBinding) holder.binding;
        binding.setVariable(variableId,listString.get(position));
        binding.executePendingBindings();
    }

    public void addAll(List<String> listString) {
        int position = listString.size() - 1;
        listString.addAll(listString);
        notifyItemRangeInserted(position, position + listString.size());
    }

    public void removeAll() {
        listString.clear();
        notifyItemRangeRemoved(0, listString.size() - 1);
    }

    @Override
    public int getItemCount() {
        return listString.size();
    }



    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ViewDataBinding binding;

        public ViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }


    }
}
